/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package themsanpham;

import javafx.scene.CustomNode;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.MouseButton;
import com.javafx.preview.control.PopupMenu;
import com.javafx.preview.control.MenuItem;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.control.Button;
import javafx.scene.layout.VBox;
import javafx.scene.control.TextBox;
import javafx.scene.layout.Stack;
import javafx.scene.shape.Rectangle;
import javafx.scene.paint.Color;
import javafx.geometry.Insets;
import java.util.ArrayList;
import javafx.scene.text.Font;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;


/**
 * @author ToanNhan
 */
public class NhomThongTinSanPham extends CustomNode {

    public-init var X: Integer;
    public-init var Y: Integer;
    public-init var Width: Integer;
    def font: Font = Font.font("Arial", 14);
    def popupMenu: PopupMenu = PopupMenu {
                items: [
                    MenuItem {
                        font:font
                        text: "Thêm thông tin"
                        action: function() {
                            var thongTinSanPham = ThongTinSanPham { Width: Width / 10 };
                            rectangleHeight += 30;
                            insert thongTinSanPham into arrayThongTinSanPham;
                        }
                    }
                ]
            }
    protected var arrayThongTinSanPham = [ThongTinSanPham { Width: Width / 10 }];
    public-read var rectangleHeight = 25;
    protected var button: Button;
    protected var textBoxName: TextBox;

    protected override function create(): Node {
        return Group {
                    content: [
                        VBox {
                            height: bind rectangleHeight + 25
                            spacing: 2
                            content: [
                                popupMenu,
                                HBox{
                                    spacing: 5
                                    content:[
                                        Label {layoutY: 7, text: "Tên nhóm thông tin", font: font},
                                        textBoxName = TextBox {
                                            width: 50, font:font
                                            }]},
                                Stack {
                                    content: [
                                        Rectangle {
                                            arcWidth: 20 arcHeight: 20
                                            height: bind rectangleHeight + 15
                                            width: bind Width
                                            stroke: Color.BISQUE
                                            fill: Color.WHITE
                                            strokeWidth: 2
                                            onMouseClicked: function(e: MouseEvent): Void {
                                                if (e.button == MouseButton.SECONDARY) {
                                                    popupMenu.show(this, HPos.CENTER, VPos.CENTER, e.x, e.y);
                                                }
                                            }
                                        }
                                        VBox {
                                            width: Width
                                            hpos: HPos.CENTER
                                            vpos: VPos.CENTER
                                            padding: Insets { left: 30 }
                                            spacing: 5
                                            content: bind arrayThongTinSanPham
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                }

    }

    public function xuatNhomThongTinSanPham(): NhomThongTinSanPhamClass{
        var nhomThongTin = new NhomThongTinSanPhamClass();
        var arr = nhomThongTin.getThongTinSanPhams();
        for (thongTin in arrayThongTinSanPham){
            arr.add(thongTin.xuatThongTinSanPham());
        }
        nhomThongTin.setTenNhomThongTinSanPham(textBoxName.text);
        return nhomThongTin;
    }

}
